// packageF/crowd_activity/goods/goods.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: [],
    Hei:0,
    imgCurrent:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.options = options;
    if(options.scene){
      let scene = decodeURIComponent(options.scene);
      var info_arr = [];
      info_arr = scene.split(',');
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split('=');
        if (chil_arr[0] == "id") {
          this.data.options.id = chil_arr[1];
        }
        if (chil_arr[0] == "uid") {
          this.data.options.uid = chil_arr[1];
        }
        if (chil_arr[0] == "mid") {
          app._setMid(chil_arr[1])
        }
      }
    }
  },
  toHome(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  swiperChange(e){
    let {current} = e.detail;
    this.setData({
      imgCurrent:current + 1
    })
  },
  imgH(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw   //等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (this.data.Hei < swiperH) {
      this.setData({
        Hei: swiperH//设置高度
      })
    }
  },
  toOrder(e) {
    console.log(e)
    let key;
    let gid = this.data.infoData.id;
    if (e.currentTarget.dataset.key) {
      key = e.currentTarget.dataset.key;
    }
    if (key) {
      if(!app._isTextEmpty(this.data.infoData.expense_mini_url)){
        wx.navigateTo({
          url: this.data.infoData.expense_mini_url,
        })
        return
      }
      if (this.data.options.uid) {
        wx.navigateTo({
          url: `/packageF/crowd_activity/order/crowd_activity_Order?id=${gid}&uid=${this.data.options.uid}&self=true`,
        })
      } else {
        wx.navigateTo({
          url: `/packageF/crowd_activity/order/crowd_activity_Order?id=${gid}&self=true`,
        })
      }
    } else {
      if (this.data.options.uid) {
        wx.navigateTo({
          url: `/packageF/crowd_activity/order/crowd_activity_Order?id=${gid}&uid=${this.data.options.uid}`,
        })
      } else {
        wx.navigateTo({
          url: `/packageF/crowd_activity/order/crowd_activity_Order?id=${gid}`,
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let urlstr = `/packageF/crowd_activity/goods/goods?id=${this.data.options.id}&mid=${mid}`
    if(this.data.options.uid){
      urlstr += `&uid=${this.data.options.uid}`
    }
    return {
      title: this.data.infoData.title,
      path: urlstr,
    }
  },
  onShareTimeline(){
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    let urlstr = `id=${this.data.options.id}&mid=${mid}`
    if(this.data.options.uid){
      urlstr += `&uid=${this.data.options.uid}`
    }
    return {
      title: this.data.infoData.title,
      // &uid=${this.data.options.uid}
      query: urlstr,
    }
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.activity.show");
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: this.data.options.id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            infoData: res.data
          })
          let sj = Date.now();
          this.setData({
            'infoData.start_at':Number(res.data.start_at+'000') - sj,
            'infoData.end_at':Number(res.data.end_at+'000') - sj
          })
          wx.setNavigationBarTitle({
            title: this.data.infoData.title,
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  }
})